/**
 * 
 */
package userService;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import constants.Constants;
import userEntity.LoginEntity;

/**
 * @author Yokesh Kumar
 *
 */

public class LoginService {

	private Connection connection;
	private PreparedStatement statement;

	/**
	 * 
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public LoginService() throws ClassNotFoundException, SQLException{
		try {
			Class.forName(Constants.JDBC_DRIVER);
			connection = DriverManager.getConnection(Constants.DB_URL,Constants.USER_NAME,Constants.PWD);
		} 
		catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw e;
		} 
	}
     
	/**
	 * This method fetches the role of the user
	 * @param login
	 * @return
	 * @throws SQLException 
	 */
	public ResultSet fetchLoginRole(LoginEntity login) throws SQLException{
		ResultSet rs = null;
		String loginId = login.getLoginId();
		String pwd = login.getPwd();
		String query = "SELECT ROLE,active FROM login WHERE LOGINID=? AND PASSWORD=? ";
		try {

			connection.setAutoCommit(false);
			statement = connection.prepareStatement(query);
			statement.setString(1, loginId);
			statement.setString(2, pwd);
			rs =  statement.executeQuery();
			connection.commit();
		} 
		catch (SQLException e1) {
			if(connection != null){
				System.out.println("transaction is rolled back");
				try {
					connection.rollback();
				} catch (SQLException e) {
					e.printStackTrace();
					throw e;
				}
			}
			e1.printStackTrace();
			throw e1;
		}
		finally{
			try {
				connection.setAutoCommit(true);
			}
			catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw e;
			}
		}
		return rs;
	}

	/**
	 * This method fetches the phone number of the user from login table
	 * @param login
	 * @return
	 * @throws SQLException 
	 */
	public ResultSet fetchUserPhoneNumber(LoginEntity login) throws SQLException {
		ResultSet rs = null;
		String loginId = login.getLoginId();
		String query = "SELECT userPhoneNumber FROM login WHERE LOGINID=? ";
		try {
			statement = connection.prepareStatement(query);
			statement.setString(1, loginId);
			rs =  statement.executeQuery();
		} 
		catch (SQLException e1) {
			e1.printStackTrace();
			throw e1;
		}
		return rs;

	}

	/**
	 * This method fetches user name from user table
	 * @param phNo
	 * @return
	 * @throws SQLException 
	 */
	public ResultSet fetchUserName(String phNo) throws SQLException {
		ResultSet rs = null;
		String query = "SELECT name FROM user WHERE phoneNumber=? ";
		try {
			statement = connection.prepareStatement(query);
			statement.setString(1, phNo);
			rs =  statement.executeQuery();
		} 
		catch (SQLException e1) {
			e1.printStackTrace();
			throw e1;
		}
		return rs;
	}

	public void closeConnection() throws SQLException{
		try{
			if(statement != null){
				statement.close();
			}
			if(connection != null){
				connection.close();
			}
		}
		catch(SQLException sqlExec){
			sqlExec.printStackTrace();
			throw sqlExec;
		}
	}
	

	/**
	 * This method fetches loginId of the user from login table
	 * @param phNo
	 * @return
	 * @throws SQLException 
	 */
	public ResultSet fetchLoginId(String phoneNumber) throws SQLException{
		ResultSet rs = null;
		String query = "Select * from login where userPhoneNumber=?";
		try {
			connection.setAutoCommit(false);
			statement = connection.prepareStatement(query);
			statement.setString(1,phoneNumber);
			rs = statement.executeQuery();
			connection.commit();
		} 
		catch (SQLException e1) {
			if(connection != null){
				System.out.println("transaction is rolled back");
				try {
					connection.rollback();
				} catch (SQLException e) {
					e.printStackTrace();
					throw e;
				}
			}
			e1.printStackTrace();
			throw e1;
		}
		
		return rs;
		
		
	}

}
